package com.lance.answer.gui.answer.gk.video.tmp;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.lance.answer.dto.CompleteInfo;
import com.lance.answer.gui.answer.Util;
import com.lance.answer.gui.answer.gk.video.dto.WatchVideoDto;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

import java.io.File;
import java.util.List;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.TimeUnit;

@Slf4j
public class BatchClickCourseInfoTask extends Thread {



    private final String driverPath;
    private final LinkedBlockingDeque<WatchVideoDto> watchVideos;

    public BatchClickCourseInfoTask(String driverPath, LinkedBlockingDeque<WatchVideoDto>  watchVideos) {
        this.driverPath = driverPath;
        this.watchVideos = watchVideos;
    }

    @SneakyThrows
    @Override
    public void run() {

        while (watchVideos.size() > 0) {
            WatchVideoDto  watchVideo = watchVideos.take();
            String userName = watchVideo.getUserName();
            String password = watchVideo.getPassword();
            log.info("任务：{} 剩余 {} 行 用户名：{} 密码：{}",
                    Thread.currentThread().getName(), watchVideos.size(), userName, password);
            System.err.println("执行任务11111");
            run(driverPath,watchVideo);
        }

        if (watchVideos.size() == 0) {
            log.info("课程所有用户均已执行完毕，系统将在20min后结束");
            TimeUnit.MINUTES.sleep(20);
            return;
        }
    }


    public static void run(String driverPath,WatchVideoDto  watchVideo) throws Exception {

        String subjectName = watchVideo.getSubjectName();
        String userName = watchVideo.getUserName();
        String password = watchVideo.getPassword();
        String loginUrl = watchVideo.getLoginUrl();
        String JS = watchVideo.getJS();
        int completeVideoSize = watchVideo.getCompleteVideoSize();
        // 校验是否完成；
        if(Util.checkCompleteVideo(subjectName,userName,completeVideoSize)){
            System.err.println("?????????????????????????????????1111111111111111111111111111");
            return;
        }

        ChromeDriver driver = Util.getDriver(driverPath);
        try {
            String url = "http://passport.ouchn.cn/Account/Login?ReturnUrl=%2Fconnect%2Fauthorize%2Fcallback%3Fclient_id%3Dstudentspace%26redirect_uri%3Dhttp%253A%252F%252Fstudent.ouchn.cn%252F%2523%252Fsignin-oidc%2523%26response_type%3Did_token%2520token%26scope%3Dopenid%2520profile%2520ouchnuser%2520ouchnstudentspaceapi%2520offline_access%26state%3Dca1cb5d0270b49fdaea508b8a846c457%26nonce%3Dd257df51b0a141ac8ed35b1caf59ef4c";
            driver.get(url);
            Thread.sleep(6000);
            Util.login(driver, userName, password);
            //Util.switchToLast(driver);
            Thread.sleep(6000);
            // 形考作业的链接 管理学

            boolean isFind = false;
            for (WebElement we : driver.findElements(By.className("media-body"))) {
                String mediaTitle = we.findElement(By.className("media-title")).getText();

//                if (!mediaTitle.contains(subjectName)) {
//                    continue;
//                }
                if (!mediaTitle.equals(subjectName)) {
                    continue;
                }
                //点击进入学习的按钮
                WebElement studyButton = we.findElement(By.cssSelector("button[class='btn bg-primary']"));
                studyButton.click();
                isFind = true;
                //Util.switchToLast(driver);
                Thread.sleep(6000);
            }

            Util.switchToLast(driver);

            int index = 1;
            //sectionname

            int h3ListSize = driver.findElements(By.cssSelector("h3[class='sectionname']")).size();

            List<String> getUrlList = Lists.newArrayList();

            for(WebElement we:driver.findElements(By.tagName("a"))){
                String u = we.getAttribute("href");
                if(!Strings.isNullOrEmpty(u)&&u.contains("view")){
                    getUrlList.add(u);
                }
            }

            for(String getUrl:getUrlList){
                Thread.sleep(3000);
                driver.get(getUrl);
            }





            //System.out.println(urlTasks.toString());
        } catch (Exception e) {
            e.printStackTrace();
            Thread.sleep(2000);
        } finally {
            try {
                driver.close();
                driver.quit();
            } catch (Exception e) {

            }
        }
    }



}
